const express = require("express");
const router = express.Router();
const pool = require("../pool");

//接口类型：产品类别查询
//接口地址：http://127.0.0.1:3000/v1/products/sort
//请求方式：GET
router.get("/sort", (req, res, next) => {
  pool.query(
    "select * from hsb_sort",
    (err, result) => {
      if (err) {
        next(err);
        return;
      }
      if (result.length > 0) {
        res.send({ code: 200, msg: "suc",result});
      } else {
        res.send({ code: 400, msg: "fail" });
      }
    }
  );
    
});
//接口类型：首页商品名称
//接口地址：http://127.0.0.1:3000/v1/products/names
//请求方式：GET
router.get("/names", (req, res, next) => {
  let obj = req.query;
  pool.query(
    'select * from hsb_products_family where home="Y"and sort=?',[obj.sort],
    (err, result) => {
      if (err) {
        next(err);
        return;
      }
      if (result.length > 0) {
        res.send({ code: 200, msg: "suc",result});
      } else {
        res.send({ code: 400, msg: "fail" });
      }
    }
  );
    
});
//接口类型：首页商品logo
//接口地址：http://127.0.0.1:3000/v1/products/logo
//请求方式：GET
router.get("/logo", (req, res, next) => {
  let obj = req.query;
  pool.query(
    'select * from hsb_products_family where home="Y" and sort=? group by fid',[obj.sort],
    (err, result) => {
      if (err) {
        next(err);
        return;
      }
      if (result.length > 0) {
        res.send({ code: 200, msg: "suc", result});
      } else {
        res.send({ code: 400, msg: "fail" });
      }
    }
  );
});

//接口类型 ：详情页商品logo
//接口地址：http://127.0.0.1:3000/v1/products/details
//请求方式：GET
router.get("/details", (req, res, next) => {
  let obj = req.query;
  pool.query("select * from hsb_products_family where sort=?",[obj.sort], (err, result) => {
    if (err) {
      next(err);
      return;
    }
    if (result.length > 0) {
      res.send({ code: 200, msg: "suc",result});
    } else {
      res.send({ code: 400, msg: "fail" });
    }
  });
});

//接口类型 ：单个商品查询
//接口地址：http://127.0.0.1:3000/v1/products/data
//请求方式：GET
//请求参数:pid 
router.get("/data", (req, res, next) => {
  let obj = req.query;
      let sql = 'select * from hsb_products where pid=?';
      pool.query(sql,[obj.pid],
        (err, result) => {
          if (err) {
            next(err);
            return;
          }
          if (result.length > 0) {
            res.send({ code: 200, msg: "suc", result});
          } else {
            res.send({ code: 400, msg: "fail" });
          }
        }
      );
});

//接口类型 ：商品数据(分页)
//接口地址：http://127.0.0.1:3000/v1/products/list
//请求方式：GET
router.get("/list", (req, res, next) => {
  let obj = req.query;
  let page=req.query.page ? req.query.page : 1
  let pagesize=20
  let rowcount
  let offset=(page-1)*pagesize
  /********************* */
  /*
  if (Object.keys(obj).length == 0) {
    pool.query("select * from hsb_products", (err, result) => {
      if (err) {
        next(err);
        return;
      }
      if (result.length > 0) {
        return res.send({ code: 200, msg: "suc", data: result });
      } else {
        res.send({ code: 400, msg: "fail" });
      }
    });
    return;
  }
  */
 /*
  if (obj.pid == "" && obj.sort == "") {
    pool.query("select * from hsb_products", (err, result) => {
      if (err) {
        next(err);
        return;
      }
      if (result.length > 0) {
        return res.send({ code: 200, msg: "suc", data: result });
      } else {
        res.send({ code: 400, msg: "fail" });
      }
    });
    return;
  }
  */
  // pid为空或pid==1
  if (obj.pid == "" || obj.pid == 1 || obj.pid == 101 || obj.pid == 200 || !obj.hasOwnProperty("pid")) {
    pool.query("select count(pid) as count from hsb_products where family_id=?",[obj.pid],(err,result)=>{
      if(err){
        next(err)
        return
      }
      rowcount = result[0].count
      let pagecount=Math.ceil(rowcount/pagesize)
      pool.query(
        'select * from hsb_products where sort=? and hot="Y" limit ?,?',
        [obj.sort,offset,pagesize],
        (err, result) => {
          if (err) {
            next(err);
            return;
          }
          if (result.length > 0) {
            res.send({ code: 200, msg: "suc", data: result ,pagecount,rowcount});
          } else {
            res.send({ code: 400, msg: "fail" });
          }
        }
      );
    })
    return;
  }

  //sort为空
  if (obj.sort !== "" && obj.pid !=='') {
    pool.query(
      "select count(pid) as count from hsb_products where family_id=?",
      [obj.pid],
      (err, result) => {
        if (err) {
          next(err);
          return;
        }
        rowcount = result[0].count
        let pagecount=Math.ceil(rowcount/pagesize)
        pool.query(
          "select * from hsb_products where family_id=? and sort=?  limit ?,?",
          [obj.pid,obj.sort,offset,pagesize],
          (err, result) => {
            if (err) {
              next(err);
              return;
            }
            if (result.length > 0) {
              res.send({ code: 200, msg: "suc", data: result,pagecount:pagecount,rowcount});
            } else {
              res.send({ code: 400, msg: "fail" });
            }
          }
        );
      }
    );
    return;
  }

  if (obj.pid !== "" && obj.sort !== "") {
    res.send({ code: 400, msg: "客户端异常" });
  }
});

//接口类型 ：搜索框接口（分页）
//接口地址：http://127.0.0.1:3000/v1/products/search
//请求方式：GET

router.get("/search", (req, res, next) => {
  let obj = req.query;
  let page =req.query.page ? req.query.page : 1 //客户端传递的page参数
  let pagesize = 20 //每页显示的记录数
  let rowcount //存储获取到的总记录数
  let offset =(page-1) * pagesize //计算从第几条数据开始返回
  let sql = `select count(pid) as count from hsb_products where product_name like '%${obj.secContent}%' or title like '%${obj.secContent}%'`;
  pool.query(sql,
    (err, result) => {
      if (err) {
        next(err);
        return;
      }
      rowcount = result[0].count
      //计算总页数
      let pagecount=Math.ceil(rowcount/pagesize)
      let sql = `select * from hsb_products where product_name like '%${obj.secContent}%' or title like '%${obj.secContent}%' limit ?,?`;
      pool.query(sql,[offset,pagesize],
        (err, result) => {
          if (err) {
            next(err);
            return;
          }
          if (result.length > 0) {
            res.send({ code: 200, msg: "suc", data: result,pagecount:pagecount,rowcount});
          } else {
            res.send({ code: 400, msg: "fail" });
          }
        }
      );

    }
  ); 
});

module.exports = router;
